package com.shenyi.shop.service;

import com.shenyi.common.entity.SysUserDO;
import com.shenyi.common.utils.ShiroUtils;
import com.shenyi.core.annotation.DataFilter;
import com.shenyi.core.utils.RRException;
import com.shenyi.shop.api.IGoodsService;
import com.shenyi.shop.dao.GoodsAttributeMapper;
import com.shenyi.shop.dao.GoodsGalleryMapper;
import com.shenyi.shop.dao.GoodsMapper;
import com.shenyi.shop.dao.ProductMapper;
import com.shenyi.shop.entity.GoodsAttributeDO;
import com.shenyi.shop.entity.GoodsDO;
import com.shenyi.shop.entity.GoodsGalleryDO;
import com.shenyi.shop.entity.ProductDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/05
 * @desc 商品
 * @see
 * @since 1.0
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private GoodsGalleryMapper goodsGalleryMapper;

    @Override
    public GoodsDO queryObject(Integer id) {
        return goodsMapper.queryObject(id);
    }

    @Override
    @DataFilter(userAlias = "nideshop_goods.create_user_id", deptAlias = "nideshop_goods.create_user_dept_id")
    public List<GoodsDO> queryList(Map<String, Object> map) {
        return goodsMapper.queryList(map);
    }

    @Override
    @DataFilter(userAlias = "nideshop_goods.create_user_id", deptAlias = "nideshop_goods.create_user_dept_id")
    public int queryTotal(Map<String, Object> map) {
        return goodsMapper.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(GoodsDO goods) {
        SysUserDO user = ShiroUtils.getUserDO();
        Map<String, Object> map = new HashMap<>();
        map.put("name", goods.getName());
        List<GoodsDO> list = queryList(map);
        if (null != list && list.size() != 0) {
            throw new RRException("商品名称已存在！");
        }
        Integer id = goodsMapper.queryMaxId() + 1;
        goods.setId(id);

        //保存产品信息
        ProductDO ProductDO = new ProductDO();
        ProductDO.setGoodsId(id);
        ProductDO.setGoodsSn(goods.getGoodsSn());
        ProductDO.setGoodsNumber(goods.getGoodsNumber());
        ProductDO.setRetailPrice(goods.getRetailPrice());
        ProductDO.setMarketPrice(goods.getMarketPrice());
        ProductDO.setGoodsSpecificationIds("");
        productMapper.save(ProductDO);

        goods.setAddTime(new Date());
        goods.setPrimaryProductId(ProductDO.getId());

        //保存商品详情页面显示的属性
        List<GoodsAttributeDO> AttributeDOList = goods.getAttributeDOList();
        if (null != AttributeDOList && AttributeDOList.size() > 0) {
            for (GoodsAttributeDO item : AttributeDOList) {
                item.setGoodsId(id);
                goodsAttributeMapper.save(item);
            }
        }

        //商品轮播图
        List<GoodsGalleryDO> galleryEntityList = goods.getGoodsImgList();
        if (null != galleryEntityList && galleryEntityList.size() > 0) {
            for (GoodsGalleryDO galleryEntity : galleryEntityList) {
                galleryEntity.setGoodsId(id);
                goodsGalleryMapper.save(galleryEntity);
            }
        }

        goods.setIsDelete(0);
        goods.setCreateUserDeptId(user.getDeptId());
        goods.setCreateUserId(user.getUserId());
        goods.setUpdateUserId(user.getUserId());
        goods.setUpdateTime(new Date());
        return goodsMapper.save(goods);
    }

    @Override
    @Transactional
    public int update(GoodsDO goods) {
        SysUserDO user = ShiroUtils.getUserDO();
        List<GoodsAttributeDO> AttributeDOList = goods.getAttributeDOList();
        if (null != AttributeDOList && AttributeDOList.size() > 0) {
            for (GoodsAttributeDO GoodsAttributeDO : AttributeDOList) {
                int result = goodsAttributeMapper.updateByGoodsIdAttributeId(GoodsAttributeDO);
                if (result == 0) {
                    goodsAttributeMapper.save(GoodsAttributeDO);
                }
            }
        }
        goods.setUpdateUserId(user.getUserId());
        goods.setUpdateTime(new Date());
        //商品轮播图
        //修改时全删全插
        List<GoodsGalleryDO> galleryEntityList = goods.getGoodsImgList();
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("goodsId", goods.getId());
        goodsGalleryMapper.deleteByGoodsId(map);

        if (null != galleryEntityList && galleryEntityList.size() > 0) {
            for (GoodsGalleryDO galleryEntity : galleryEntityList) {
                galleryEntity.setGoodsId(goods.getId());
                goodsGalleryMapper.save(galleryEntity);
            }
        }
        return goodsMapper.update(goods);
    }

    @Override
    public int delete(Integer id) {
        SysUserDO user = ShiroUtils.getUserDO();
        GoodsDO GoodsDO = queryObject(id);
        GoodsDO.setIsDelete(1);
        GoodsDO.setIsOnSale(0);
        GoodsDO.setUpdateUserId(user.getUserId());
        GoodsDO.setUpdateTime(new Date());
        return goodsMapper.update(GoodsDO);
    }

    @Override
    @Transactional
    public int deleteBatch(Integer[] ids) {
        int result = 0;
        for (Integer id : ids) {
            result += delete(id);
        }
        return result;
    }

    @Override
    @Transactional
    public int back(Integer[] ids) {
        SysUserDO user = ShiroUtils.getUserDO();
        int result = 0;
        for (Integer id : ids) {
            GoodsDO GoodsDO = queryObject(id);
            GoodsDO.setIsDelete(0);
            GoodsDO.setIsOnSale(1);
            GoodsDO.setUpdateUserId(user.getUserId());
            GoodsDO.setUpdateTime(new Date());
            result += goodsMapper.update(GoodsDO);
        }
        return result;
    }

    @Override
    public int enSale(Integer id) {
        SysUserDO user = ShiroUtils.getUserDO();
        GoodsDO GoodsDO = queryObject(id);
        if (1 == GoodsDO.getIsOnSale()) {
            throw new RRException("此商品已处于上架状态！");
        }
        GoodsDO.setIsOnSale(1);
        GoodsDO.setUpdateUserId(user.getUserId());
        GoodsDO.setUpdateTime(new Date());
        return goodsMapper.update(GoodsDO);
    }

    @Override
    public int unSale(Integer id) {
        SysUserDO user = ShiroUtils.getUserDO();
        GoodsDO GoodsDO = queryObject(id);
        if (0 == GoodsDO.getIsOnSale()) {
            throw new RRException("此商品已处于下架状态！");
        }
        GoodsDO.setIsOnSale(0);
        GoodsDO.setUpdateUserId(user.getUserId());
        GoodsDO.setUpdateTime(new Date());
        return goodsMapper.update(GoodsDO);
    }

}
